package com.app.juyanresturantapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class ProductListActivity extends AppCompatActivity {
    private static final String TAG = "ProductListActivity";

    ArrayList<Foods> foodList;//食物列表
    ListView mLvFoods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);

        initData();// 获取数据
        initView();// 初始化视图
        initEvent();// 初始化事件

    }

    private void initEvent() {

    }

    private void initView() {
        mLvFoods = findViewById(R.id.lv_foods);
    }

    private void initData() {
        // 获取数据
        // 1 创建一个请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        // 2 创建一个请求
        String url = "http://182.61.26.165:8080/JuYanResturantApp/food/list";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("AppDebug", "onResponse: " + jsonObject.toString());
                //解析数据并呈现数据
                // json --> ArrayList   -->ListView
                // 创建实体类
                // 解析数据
                try {
                    JSONArray list = jsonObject.getJSONArray("list");
                    //食物列表
                    foodList = new ArrayList();
                    Log.e(TAG, "onResponse: "+list.length());
                    for (int i = 0; i < list.length(); i++) {
                        // 空的食物类
                        Foods foods = new Foods();
                        // 设置食物的信息
                        JSONObject object = list.getJSONObject(i);//第i项数据
                        foods.setId(object.getInt("id"));
                        foods.setName(object.getString("name"));
                        foods.setDescription(object.getString("description"));
                        foods.setLabel(object.getString("laber"));
                        foods.setPrice((float) object.getDouble("price"));
                        foods.setIcon(object.getString("icon"));
                        // 将foods加入foodList中
                        foodList.add(foods);
                        //ArrayList   -->ListView
                    }
                    updateView();//更新界面  ERROR 要在数据解析完成后更新界面


                } catch (JSONException e) {
                    //Log.e(TAG, "onResponse: ",e );
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("AppDebug", "onErrorResponse: " + volleyError);
            }
        });

        // 3 将创建的请求添加到请求队列中
        requestQueue.add(jsonObjectRequest);


    }

    private void updateView() {
        // 创建适配器对象
        //　定义适配器类
        // 创建适配器对象
        MyAdapter adapter = new MyAdapter();
        //　绑定到ListView
        mLvFoods.setAdapter(adapter);

    }

    //　定义适配器类
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return foodList.size();//要显示的数据数量
        }

        @Override
        public Object getItem(int position) {// 要显示的第position位置的数据

            return foodList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 数据项 和 显示的控件  要 对应  起来
            //获取数据
            Foods foods = foodList.get(position);
            // 显示的控件
            View view = View.inflate(ProductListActivity.this,
                    R.layout.list_item_layout,
                    null);
            ImageView imageView = view.findViewById(R.id.iv_icon);// 图片控件
            TextView tvName = view.findViewById(R.id.tv_name);
            // ......
            // 数据和控件对应起来
            tvName.setText(foods.getName());
            // ......
            Picasso.with(ProductListActivity.this) // 1 上下文 2 加载图片 3 设置显示的位置
                    //.load("http://182.61.26.165:8080/JuYanResturantApp/images/"+foods.getIcon())
                    .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1570550879239&di=0a5ae03e0300fd6f1aa1254d68102871&imgtype=0&src=http%3A%2F%2Ftvax4.sinaimg.cn%2Fcrop.0.0.512.512.1024%2Fc2c2f4dely8fsgflf92e5j20e80e8myn.jpg")
                    .into(imageView);

            return view;//返回控件
        }
    }
}
